package com.ymm.utils.log;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.alibaba.fastjson.JSON;
import com.ymm.annotation.SystemLog;
import com.ymm.domain.entity.Log;
import com.ymm.enums.SystemContains;
import org.aspectj.lang.ProceedingJoinPoint;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 椰咩咩咩
 * @version 1.0-SNAPSHOT
 * @date 2023/1/20 13:05
 * @Description 日志信息的相关工具类
 */
public class LogEntityUtils {

    public static Log getLogEntity(HttpServletRequest request, SystemLog systemLog, ProceedingJoinPoint joinPoint){
        Log log=new Log();
        String uaStr=request.getHeader("User-Agent");
        UserAgent ua = UserAgentUtil.parse(uaStr);
        log.setBrowser( ua.getBrowser().toString());
        log.setBrowserVersion(ua.getVersion());
        log.setClassMethod(joinPoint.getSignature().getDeclaringType()+ joinPoint.getSignature().getName());
        log.setBusinessName(systemLog.businessName());
        log.setEngine(ua.getEngine().toString());
        log.setEngineVersion(ua.getEngineVersion());
        log.setOs( ua.getOs().toString());
        log.setIsMobile(ua.isMobile()? SystemContains.YES:SystemContains.NO);
        log.setIp(request.getRemoteHost());
        log.setHttpMethod(request.getMethod());
        log.setRequestArgs(JSON.toJSONString(joinPoint.getArgs()));
        log.setUrl(request.getRequestURI());
        return log;
    }

}
